package com.lz.autotest.project.testmanage.autoTest.service.impl;

import com.lz.autotest.common.entity.ResponseVo;
import com.lz.autotest.common.utils.SecurityUtils;
import com.lz.autotest.project.testmanage.autoTest.entity.TsstAutotestItem;
import com.lz.autotest.project.testmanage.autoTest.entity.TsstAutotestItemVo;
import com.lz.autotest.project.testmanage.autoTest.mapper.TsstAutotestItemMapper;
import com.lz.autotest.project.testmanage.autoTest.service.TsstAutotestItemService;
import com.lz.autotest.project.testmanage.infmanage.domain.SstInf;
import com.lz.autotest.project.testmanage.infmanage.mapper.InfManageMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author: X
 * @date: Created in 2023/6/15 16:52
 * @Description:
 */

@Service
public class TsstAutotestItemServiceImpl implements TsstAutotestItemService {

    @Resource
    private TsstAutotestItemMapper tsstAutotestItemMapper;

    @Resource
    private InfManageMapper infManageMapper;


    @Override
    public ResponseVo insert(TsstAutotestItem tsstAutotestItem) {
        // valid
        if (tsstAutotestItem == null) {
            return ResponseVo.failure("必要参数缺失");
        }
        tsstAutotestItemMapper.insert(tsstAutotestItem);
        return ResponseVo.success();
    }


    @Override
    public ResponseVo delete(Long id) {
        int ret = tsstAutotestItemMapper.delete(id);
        return ret > 0 ? ResponseVo.success() : ResponseVo.failure();
    }


    @Override
    public ResponseVo update(TsstAutotestItem tsstAutotestItem) {
        int ret = tsstAutotestItemMapper.update(tsstAutotestItem);
        return ret > 0 ? ResponseVo.success() : ResponseVo.failure();
    }


    @Override
    public TsstAutotestItem load(int id) {
        return tsstAutotestItemMapper.load(id);
    }

    @Override
    public List<SstInf> getMenuDetail(Map<String, Object> param) {
        return infManageMapper.getMenuDetail(param);
    }

    @Override
    public void saveAutoTestItem(TsstAutotestItemVo vo) {
        Long menuId = vo.getMenuId();
        List<SstInf> infs = vo.getInfs();
        String status = vo.getStatus();
        List<TsstAutotestItem> tsstAutotestItems = new ArrayList<>();
        for (SstInf inf : infs) {
            TsstAutotestItem tsstAutotestItem = new TsstAutotestItem();
            tsstAutotestItem.setMenuId(menuId);
            tsstAutotestItem.setInfId(Long.parseLong(inf.getInfId()));
            tsstAutotestItem.setStatus(status);
            tsstAutotestItem.setCreateBy(SecurityUtils.getUsername());
            tsstAutotestItems.add(tsstAutotestItem);
        }
        tsstAutotestItemMapper.batchInsetATI(tsstAutotestItems);
    }

}